package fashionshop.bll;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Vector;

import sun.misc.Sort;

import fashionshop.dal.CustomerDAL;
import fashionshop.dal.LevelDAL;
import fashionshop.dal.ProductDAL;


public class CustomerBLL {
	
	private int id;
	private int levelId;
	private String name;
	private String phone;
	private String address;
	private int score;
	private int active;
	
	public CustomerBLL() {
		this.levelId = 1;
		this.name = "";
		this.phone = "";
		this.address = "";
		this.score = 0;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getLevelId() {
		return levelId;
	}
	public void setLevelId(int levelId) {
		this.levelId = levelId;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public int getScore() {
		return score;
	}
	public void setScore(int score) {
		this.score = score;
	}
	public int getActive() {
		return active;
	}
	public void setActive(int active) {
		this.active = active;
	}
	
	public static Vector loadAllCustomer() throws Exception {
		
		Vector customerTableRecords = new Vector();
		ResultSet result = CustomerDAL.LoadAll();
		while (result.next()){
			Vector record = new Vector();
            record.add(result.getInt("ID"));
            record.add(result.getString("Name"));
            record.add(result.getString("Phone"));
            record.add(result.getString("Address"));
            record.add(result.getString("Level"));
            record.add(result.getInt("Score"));
			
            customerTableRecords.add(record);
		}
		return customerTableRecords;
	}
	
	
	public boolean SaveAll(Vector customerTableRecords) throws Exception {
		
		boolean flag = true;
		for(int i = 0 ; i < customerTableRecords.size(); i++){
			Vector vt = (Vector) customerTableRecords.get(i);
			if(vt.get(0).toString().isEmpty() || vt.get(0).toString() == null || vt.get(0).toString().equals("")){
				CustomerBLL customer = new CustomerBLL();
				customer.setName(vt.get(1).toString());
				customer.setPhone(vt.get(2).toString());
				customer.setAddress(vt.get(3).toString());
				if(CustomerDAL.Insert(customer) == 0){
					flag = false;
					break;
				}
			}
			else{
				CustomerBLL customer = new CustomerBLL();
				customer.setId(Integer.parseInt(vt.get(0).toString()));
				customer.setName(vt.get(1).toString());
				customer.setPhone(vt.get(2).toString());
				customer.setAddress(vt.get(3).toString());
				if(CustomerDAL.Update(customer) == 0){
					flag = false;
					break;
				}
			}
		}
			
		return flag;
	}
	
	public static int getIdFromName(String name) throws Exception {
		ResultSet result = LevelDAL.LoadIDByName(name);
		int id = 0;
		while (result.next()){
			id = result.getInt("ID");
		}
		return id;
	}
	
	public Vector AdvancedSearch(CustomerBLL customer, int scoreForm, int scoreTo) throws Exception {
		
		Vector customerTableRecords = new Vector();
		ResultSet result = CustomerDAL.LoadByCriteria(customer, scoreForm, scoreTo);
		while (result.next()){
			Vector record = new Vector();
			record.add(result.getInt("ID"));
            record.add(result.getString("Name"));
            record.add(result.getString("Phone"));
            record.add(result.getString("Address"));
            record.add(result.getString("Level"));
            record.add(result.getInt("Score"));
			
            customerTableRecords.add(record);
		}
		return customerTableRecords;
	}
	
	public Vector BasicSearch(String keyword) throws Exception {
		
		Vector customerTableRecords = new Vector();
		ResultSet result = CustomerDAL.LoadByKeywork(keyword);
		while (result.next()){
			Vector record = new Vector();
			record.add(result.getInt("ID"));
            record.add(result.getString("Name"));
            record.add(result.getString("Phone"));
            record.add(result.getString("Address"));
            record.add(result.getString("Level"));
            record.add(result.getInt("Score"));
			
            customerTableRecords.add(record);
		}
		return customerTableRecords;
	}
	
	
	public void UpdateScore(int customerID, BigDecimal orderSum) throws Exception{
		int score = Integer.parseInt(orderSum.divide(new BigDecimal("100000"), BigDecimal.ROUND_DOWN).toString());
		int levelID = 0;
		ResultSet result = CustomerDAL.LoadByID(customerID);
		int tmp = 0;
		while (result.next()){
			tmp = result.getInt("Score");
		}
		score = score + tmp;
		levelID = this.GetLevelByScore(score);
		
		CustomerBLL customerBll = new CustomerBLL();
		customerBll.setScore(score);
		customerBll.setLevelId(levelID);
		customerBll.setId(customerID);
		CustomerDAL.UpdateScoreAndLevel(customerBll);
		
		System.out.println(score);
		System.out.println(levelID);
	}
	
	public int GetLevelByScore(int score) throws Exception{
		ResultSet result = LevelDAL.LoadAll();
		int levelID = 1;
		while (result.next()){
			if(score >= result.getInt("Score")){
				levelID = result.getInt("ID");
			}
			else{
				break;
			}
		}
		return levelID;
	}
}
